﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Models.Data;

namespace BlackBBS.Infrastructure.Repository
{
    public class XMLServerRepository : IServerRepository
    {
        public IRepository<BBSListEntry> BBSLists { get; set; }
        public IRepository<CallHistory> CallHistories { get; set; }
        public IRepository<ChatRoom> ChatRooms { get; set; }
        public IRepository<Email> Emails { get; set; }
        public IRepository<ExtProgram> ExtPrograms { get; set; }
        public IRepository<File> Files { get; set; }
        public IRepository<FileArea> FileAreas { get; set; }
        public IRepository<Group> Groups { get; set; }
        public IRepository<MessageArea> MessageAreas { get; set; }
        public IRepository<Message> Messages { get; set; }
        public IRepository<Network> Networks { get; set; }
        public IRepository<Session> Sessions { get; set; }
        public IRepository<User> Users { get; set; }

        public XMLServerRepository()
        {
        }

        public void Init(ConfigSettings settings)
        {
            this.BBSLists = new XMLRepository<BBSListEntry>(this.BuildFilename<BBSListEntry>(settings.BBSRootPath));
            this.CallHistories = new XMLRepository<CallHistory>(this.BuildFilename<CallHistory>(settings.BBSRootPath));
            this.ChatRooms = new XMLRepository<ChatRoom>(this.BuildFilename<ChatRoom>(settings.BBSRootPath));
            this.Emails = new XMLRepository<Email>(this.BuildFilename<Email>(settings.BBSRootPath));
            this.ExtPrograms = new XMLRepository<ExtProgram>(this.BuildFilename<ExtProgram>(settings.BBSRootPath));
            this.Files = new XMLRepository<File>(this.BuildFilename<File>(settings.BBSRootPath));
            this.FileAreas = new XMLRepository<FileArea>(this.BuildFilename<FileArea>(settings.BBSRootPath));
            this.Groups = new XMLRepository<Group>(this.BuildFilename<Group>(settings.BBSRootPath));
            this.MessageAreas = new XMLRepository<MessageArea>(this.BuildFilename<MessageArea>(settings.BBSRootPath));
            this.Messages = new XMLRepository<Message>(this.BuildFilename<Message>(settings.BBSRootPath));
            this.Networks = new XMLRepository<Network>(this.BuildFilename<Network>(settings.BBSRootPath));
            this.Sessions = new XMLRepository<Session>(this.BuildFilename<Session>(settings.BBSRootPath));
            this.Users = new XMLRepository<User>(this.BuildFilename<User>(settings.BBSRootPath));

        }

        public void Dispose()
        {
            this.BBSLists.Dispose();
            this.CallHistories.Dispose();
            this.ChatRooms.Dispose();
            this.Emails.Dispose();
            this.ExtPrograms.Dispose();
            this.Files.Dispose();
            this.FileAreas.Dispose();
            this.Groups.Dispose();
            this.MessageAreas.Dispose();
            this.Messages.Dispose();
            this.Networks.Dispose();
            this.Sessions.Dispose();
            this.Users.Dispose();
        }

        string BuildFilename<T>(string rootPath)
        {
            return System.IO.Path.Combine(System.IO.Path.Combine(rootPath, "db"), typeof(T).ToString().Replace(".", "_") + ".xml");
        }
    }
}
